"""


k 4 2 4 3

A = 3 2 4 1
1-3 4 2 3 1
1-4 1 3 2 4
2-2 3 1 2 4
2-3 2 1 3 4
3-1 2 1 3 4
3-2 1 2 3 4
4-1 1 2 3 4
4-1 1 2 3 4
res: record current max num reverse position
index: not sort max num position in array
nums: array


for each array, find max num, reverse this nums to array first, add this position to array

reverse array, reverse length is len(nums)- operate  times - 1
"""


def pancake_sort(nums):
    res = []
    for i in range(len(nums) - 1, 0, -1):
        index = nums.index(max(nums[:i + 1]))
        nums[:index + 1] = nums[:index + 1][::-1]
        res.append(index + 1)
        nums[:i + 1] = nums[:i + 1][::-1]
        res.append(i + 1)
    return res, nums


array = [3, 2, 4, 1]
print(pancake_sort(array))
